<?php
//$Id: DbTable.php 609 2012-12-17 01:23:51Z tomqin@gmail.com $

/**
 * Part of the framework.
 *
 * @package     Library
 * @version     1.0
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */


class DbTable
{
    protected $_table;
    protected $_pk;
    protected $_fields = array();
    protected $_data = array();
    protected $_bind = array();

    /*
        @param array   key 0 for pk; key 1 for fields
    */
    public function __construct($table, array $struct = array()) {
        $this->_table = Db::table(strtolower($table));

        if(is_array($struct)) {
            $this->_pk     = $struct[0];
            $this->_fields = $struct[1];
        }
    }

    public function pk() {
        return $this->_pk;
    }

    public function fields() {
        return $this->_fields;
    }

    public function set($key, $value) {
        $this->_data[$key] = $value;
        return $this;
    }

    public function arr($data) {
        $this->_data = array();
        foreach($this->_fields as $key) {
            if(isset($data[$key]))
                $this->_data[$key] = $data[$key];
        }
        if(isset($data[$this->_pk]))
            $this->_data[$this->_pk] = $data[$this->_pk];
        return $this;
    }

    public function select($where) {
        $sql = "SELECT * FROM `{$this->_table}` WHERE ". Db::where($where);
        return Db::getAll($sql);
    }

    public function get($idx) {
        $sql = "SELECT * FROM `{$this->_table}` WHERE ";
        if(is_array($idx)) {
            $sql .= "`{$this->_pk}` IN(" .join(',', $idx). ")";
            return Db::getAll($sql);
        }else{
            $sql .= "`{$this->_pk}`= '$idx'";
            return Db::query($sql)->fetch_assoc();
        }
    }

    public function update($id) {
        $_data = Arr::filterKeys($this->_data, $this->_fields);
        return Db::update($this->_table, $_data, array($this->_pk => $id));
    }

    public function insert() {
        $_data = Arr::filterKeys($this->_data, $this->_fields + array($this->_pk));
        return Db::insert($this->_table, $_data);
    }

    public function replace($data) {
        $_data = Arr::filterKeys($this->_data, $this->_fields + array($this->_pk));
        return Db::insert($this->_table, $_data, true);
    }

    public function delete($idx) {
        if(is_array($idx)) {
            $where = "`{$this->_pk}` IN (" .join(',', $idx). ")";
        }else{
            $where = array($this->_pk => $idx);
        }
        return Db::delete($this->_table, $where);
    }
/*
    public function execPrepare($type){
        if(strtolower($type) == 'insert'){
            $sql = "INSERT INTO `%s` SET "
            $comm = '';
            foreach($this->_bind as $k=>$v){
                $sql .= $comm . "`{$k}` = ?";
                $comm = ',';
            }
        }
    }

    public function bind($name, &$value){
        $this->_bind[$name] &= $value;
    }
*/
    /**
     * 缓存所有表结构
     *
     * @param array  需要排除的表

    public static function cacheAllTableStruct(array $exclude = array()) {
        $res = Db::query("SHOW TABLES");
        while($row = $res->fetch_row()) {
            $table = $row[0];
            $struct[$table] = self::getTableStruct($table);
        }
        Cache::set(strtolower(__CLASS__), $struct);
    }*/

    /**
     * 取得表结构

    public static function getTableStruct($table) {
        $columns = Db::getAll("SHOW FULL COLUMNS FROM $table");
        foreach($columns as $col) {
            if($col['Key'] == 'PRI' && !isset($struct[0])) {
                $struct[0] = $col['Field'];
                continue;
            }
            $struct[1][] = $col['Field'];
        }
        return $struct;
    }*/
}
